﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Windows.Forms;
using Digihail.GOE.Utils;

namespace BaiduFanyi
{
    public partial class Form1 : Form
    {

        private TranslateResult m_TranslateResult;
        public Form1()
        {
            InitializeComponent();
        }

        private string GetFromValue()
        {
            StringBuilder sbValue = new StringBuilder();
            Dictionary<string, PropertyInfo> DictValues = new Dictionary<string, PropertyInfo>();
            foreach (var line in txtProperty.Lines)
            {

                if (!DictValues.ContainsKey(line) && string.IsNullOrWhiteSpace(line) == false)
                {
                    DictValues.Add(line, new PropertyInfo());
                    sbValue.AppendLine(line);
                }
            }
            return sbValue.ToString();
        }

        /// <summary>
        /// 连百度翻译生成CS文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBuild_Click(object sender, EventArgs e)
        {
            StringBuilder sbValue = new StringBuilder();
            Dictionary<string, PropertyInfo> DictValues =new Dictionary<string, PropertyInfo>();
            foreach (var line in txtProperty.Lines)
            {
               
                if (!DictValues.ContainsKey(line)&&string.IsNullOrWhiteSpace(line)==false)
                {
                    DictValues.Add(line,new PropertyInfo());
                    sbValue.AppendLine(line);
                }
            }

            TranslateResult translateResult = TranslateHelp.Translate(GetFromValue());
            m_TranslateResult = translateResult;
            foreach (Trans_Result transResult in translateResult.trans_result)
            {
                if (DictValues.ContainsKey(transResult.src))
                {
                    string prepertyName = GetPrepertyName(transResult.dst);
                    DictValues[transResult.src].PrepertyName = prepertyName;
                }
            }
            this.txtClassFile.Text = GetFillValue(DictValues).ToString();
        }

        private string GetPrepertyName(string dst)
        {
            char[] words = dst.ToCharArray();
          
            List<char> lisResultWords=new List<char>();

            //首字母大写
            if (words.Length > 0)
            {
                words[0]= char.ToUpper(words[0]);
            }

            for (int i = 0; i < words.Length; i++)
            {
                //32是空格的意思
                if (words[i] == 32)
                {
                    if (i < words.Length - 2)
                    {
                        //第二个单词 首字母大写(如Space number 应该为SpaceNumber)
                        words[i + 1] = char.ToUpper(words[i + 1]);
                    }
                   continue;
                }

                //过滤掉非字母
                if (!char.IsLetter(words[i]))
                {
                    if (i < words.Length - 2)
                    {
                        //第二个单词 首字母大写(如Space number 应该为SpaceNumber)
                        words[i + 1] = char.ToUpper(words[i + 1]);
                    }
                    continue;
                }
                    lisResultWords.Add(words[i]);
            }
           
            string result = new string(lisResultWords.ToArray());
           
            return result;
        }

        private StringBuilder GetFillValue(Dictionary<string, PropertyInfo> dictValues)
        {
            StringBuilder sbValue=new StringBuilder();

            string equals = @"=""""";
            foreach (string key in dictValues.Keys)
            {
                sbValue.AppendLine($"/// <summary>");
                sbValue.AppendLine($"/// {key}");
                sbValue.AppendLine($"/// </summary>");
                if (dictValues[key].TypeName == "string")
                {
                    sbValue.AppendLine($"private {dictValues[key].TypeName} m_{dictValues[key].PrepertyName}{equals};");
                }
                else
                {
                    sbValue.AppendLine($"private {dictValues[key].TypeName} m_{dictValues[key].PrepertyName};");
                }
                
                sbValue.AppendLine();
                sbValue.AppendLine($"/// <summary>");
                sbValue.AppendLine($"///{key}");
                sbValue.AppendLine($"/// </summary>");
                sbValue.AppendLine($"public {dictValues[key].TypeName} {dictValues[key].PrepertyName}");
                sbValue.AppendLine("{");
                sbValue.AppendLine($"get {{ return m_{dictValues[key].PrepertyName}; }}");
                sbValue.AppendLine($"set");
                sbValue.AppendLine("{");
                sbValue.AppendLine($"m_{dictValues[key].PrepertyName}= value; ");
                sbValue.AppendLine($"this.RaisePropertyChanged(()=> {dictValues[key].PrepertyName});");
                sbValue.AppendLine("}");
                sbValue.AppendLine("}");
                sbValue.AppendLine();
            }
            
            return sbValue;

        }

        /// <summary>
        /// 得到译文
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnGetTranslate_Click(object sender, EventArgs e)
        {
            if (m_TranslateResult == null)
            {
                TranslateResult translateResult = TranslateHelp.Translate(GetFromValue());
                m_TranslateResult = translateResult;
            }

            StringBuilder sbValue = new StringBuilder();
            foreach (Trans_Result transResult in m_TranslateResult.trans_result)
            {
                string prepertyName = GetPrepertyName(transResult.dst);
                sbValue.AppendLine($"{prepertyName}");
            }
            this.txtClassFile.Text = sbValue.ToString();
        }

        private void btn加斜杠_Click(object sender, EventArgs e)
        {
            if (m_TranslateResult == null)
            {
                TranslateResult translateResult = TranslateHelp.Translate(GetFromValue());
                m_TranslateResult = translateResult;
            }

            StringBuilder sbValue = new StringBuilder();
            foreach (Trans_Result transResult in m_TranslateResult.trans_result)
            {
                sbValue.AppendLine($"//{transResult.src}({transResult.dst})");
                sbValue.AppendLine();
                sbValue.AppendLine();
            }
            this.txtClassFile.Text = sbValue.ToString();
        }

        private void txtProperty_TextChanged(object sender, EventArgs e)
        {
            m_TranslateResult = null;
        }

        /// <summary>
        /// 生成Winform数据绑定的数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnWinformDataBind_Click(object sender, EventArgs e)
        {
            if (m_TranslateResult == null)
            {
                TranslateResult translateResult = TranslateHelp.Translate(GetFromValue());
                m_TranslateResult = translateResult;
            }

            StringBuilder sbValue = new StringBuilder();
            foreach (Trans_Result transResult in m_TranslateResult.trans_result)
            {
                sbValue.AppendLine($"//{transResult.src}({transResult.dst})");
                //数据绑定语句
                string prepertyName = GetPrepertyName(transResult.dst);
                sbValue.AppendLine($"this.txt{prepertyName}.DataBindings.Add(\"Text\", {this.txtObjectName.Text},");
                sbValue.AppendLine($"    PropertyHelp.GetPropertyName<{txtClassName.Text}>(x => x.{prepertyName}));");
                sbValue.AppendLine();
            }
            this.txtClassFile.Text = sbValue.ToString();
           
        }
    }
}
